.TH std::chrono::operator+,std::chrono::operator-(std::chrono::year) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::chrono::operator+,std::chrono::operator-(std::chrono::year) \- std::chrono::operator+,std::chrono::operator-(std::chrono::year)

.SH Synopsis
   constexpr std::chrono::year operator+( const std::chrono::year& y,
                                          const std::chrono::years&   \fB(1)\fP \fI(since C++20)\fP
   ys ) noexcept;
   constexpr std::chrono::year operator+( const std::chrono::years&
   ys,                                                                \fB(2)\fP \fI(since C++20)\fP
                                          const std::chrono::year& y
   ) noexcept;
   constexpr std::chrono::year operator-( const std::chrono::year& y,
                                          const std::chrono::years&   \fB(3)\fP \fI(since C++20)\fP
   ys ) noexcept;
   constexpr std::chrono::years operator-( const std::chrono::year&
   y1,                                                                \fB(4)\fP \fI(since C++20)\fP
                                           const std::chrono::year&
   y2 ) noexcept;

   1,2) Adds ys.count() years to y.
   3) Subtracts ys.count() years from y.
   4) Returns the difference in years between y1 and y2.

.SH Return value

   1,2) std::chrono::year(int(y) + ys.count())
   3) std::chrono::year(int(y) - ys.count())
   4) std::chrono::years(int(y1) - int(y2))

.SH Notes

   If the resulting year value for (1-3) is outside the range [-32767, 32767], the
   actual value stored is unspecified.

   The result of subtracting two year values is a duration of type std::chrono::years.
   This duration unit represents the length of the average Gregorian year, and the
   resulting duration bears no relationship to the number of days in the particular
   years represented by the operands. For example, the result of 2018y - 2017y is
   std::chrono::years\fB(1)\fP, which represents 365.2425 days, not 365 days.

.SH Example


// Run this code

 #include <cassert>
 #include <chrono>

 int main()
 {
     std::chrono::year y{2020};

     y = std::chrono::years(12) + y; // overload (2): duration + time point
     assert(y == std::chrono::year(2032));

     y = y - std::chrono::years(33); // overload (3): time point - duration
     assert(y == std::chrono::year(1999));

     // y = std::chrono::years(33) - y; // not supported: duration - time point

     using namespace std::chrono;
     constexpr std::chrono::years ys = 2025y - 2020y; // overload (4)
     static_assert(ys == std::chrono::years(5));
 }

.SH See also

   operator++
   operator++(int) increments or decrements the month
   operator--      \fI(public member function of std::chrono::month)\fP
   operator--(int)
   operator+=      adds or subtracts a number of months
   operator-=      \fI(public member function of std::chrono::month)\fP
